<?php
/**
* Load all transactions for a given category
*
* @author       Fabrice Douteaud <admin@clearbudget.net>
* @package      actions
* @access       public
*/

/***********************************************************************

  Copyright (C) 2008  Fabrice Douteaud (admin@clearbudget.net)

    This file is part of ClearBudget.

    ClearBudget is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClearBudget is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClearBudget.  If not, see <http://www.gnu.org/licenses/>.


************************************************************************/
//prevent direct access
if(!defined('ENGINEON')) die('Direct access forbidden');

$db = class_db::getInstance();
$report = class_report::getInstance();

// if filter is set then we set the selected flters
$categorySelected = $context->categorySelected;
if($categorySelected === null) $categorySelected = false;
$monthSelected = $context->monthSelected;
if($monthSelected === null) $monthSelected = false;

// we need to reverse the month as sqlite store this in US format and we use EU format :-(
if($monthSelected!=false) {
  $monthSelected = explode('-', $monthSelected);
  $monthSelected = $monthSelected[1].'-'.$monthSelected[0].'-%';
  }
  
// get the debit flag
$debit = $context->debit;
if($debit === null) $debit = 0;

// setup the query
$db->addFields('id');
$db->addFields('operationDate');
$db->addFields('category');
$db->addFields('categoryName');
$db->addFields('amount');
$db->addFields('debit');
$db->addFields('payee');
$db->addFields('memo');
$db->addFields('comments');
if($categorySelected != false) {
  $db->addWhere('category', '=', $categorySelected);
  $db->addWhere('debit', '=', $debit);
  if($monthSelected != false) $db->addWhere('operationDate', 'like', $monthSelected);
  // execute
  $count = $db->select('v_items', true);
  }
else {
  $db->addWhere('debit', '=', $debit);
  if($monthSelected != false) $db->addWhere('operationDate', 'like', $monthSelected);
  // execute
  $count = $db->select('v_items_unfiled', true);
  }
$results = $db->fetchAllRows();

// get the category name is we have a category
$category = '';
if($categorySelected > 0) {
  $category = $report->getCategoryName($categorySelected);
  }
?>